//
//  VisitorLoginView.swift
//  Sina(微博)
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    optional func userWillLogin()
    
    optional func userWillRegister()
    
}



class VisitorLoginView: UIView {
    
    weak var visitroViewDelegate: VisitorLoginViewDelegate?
    
    
    func setupInfo(tipText: String, imageName: String?) {
        tipLabel.text = tipText
        if let name = imageName {
            //不是首页
            circleView.image = UIImage(named: name)
            //隐藏大房子
            largeIcon.hidden = true
            //将circleView移动到视图最顶层
            bringSubviewToFront(circleView)
        } else {
            //就是首页
            //就开始做动画
            startAnimation()
        }
    }
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画属性
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        anim.toValue = 2 * M_PI
        //当动画完毕或者页面失去活跃状态动画不移除图层
        anim.removedOnCompletion = false
        //添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
    }

    
    override init(frame: CGRect) {
        //调用super.init
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

       
    private func setupUI(){
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        for subView in subviews {
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        //添加圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil , views: ["backView" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil , views: ["backView" : backView,"regBtn" : registerBtn]))
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //登录按钮与注册按钮的监听事件
        registerBtn.addTarget(self, action: "registerBtnCilck", forControlEvents: UIControlEvents.TouchUpInside)
        loginBtn.addTarget(self, action: "loginBtnCilck", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        }
    //按钮的点击事件
    @objc private func registerBtnCilck() {
        visitroViewDelegate?.userWillRegister?()
    }
    @objc private func loginBtnCilck() {
       visitroViewDelegate?.userWillLogin?()
    }

    
    
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var largeIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var tipLabel: UILabel = {
        let tLabel = UILabel()
        tLabel.text = "关注一些人，回这里看看有什么惊喜!"
        tLabel.textColor = UIColor.darkGrayColor()
        tLabel.font = UIFont.systemFontOfSize(14)
        tLabel.numberOfLines = 0
        tLabel.sizeToFit()
        return tLabel
    }()
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        btn.sizeToFit()
        
        return btn
    }()
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.sizeToFit()
        
        return btn
    }()
    
    
    
    
    
}
